package _99练习;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

<<<<<<< HEAD
/**
 * @author ZhiCheng Qian
 * @since 2021/11/30 21:06
 */
=======

>>>>>>> 1c69d32fc055e0010098b09f71795769d9a12acd
public class BreakCodeGUI extends JFrame {

    static String passWord;
    static String breakPassWord;
    static JLabel l1;
    static JLabel l2;

    public BreakCodeGUI() {
        this.setLayout(null);

        JButton btn1 = new JButton("生成密码");
        JButton btn2 = new JButton("破解密码");

        btn1.setBounds(200, 200, 100, 100);
        btn2.setBounds(400, 200, 100, 100);

        this.add(btn1);
        this.add(btn2);

        l1 = new JLabel();
        l2 = new JLabel();
        Font font = new Font("微软雅黑", Font.PLAIN, 16);
        l1.setBounds(200, 350, 100, 100);
        l2.setBounds(400, 350, 100, 100);
        l1.setFont(font);
        l2.setFont(font);
        this.add(l1);
        this.add(l2);

        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                passWord = generateCode();
                l1.setText(passWord);
            }
        });


        btn2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            breakPassWord = generateCode();
                            if (passWord.equals(breakPassWord)) {
                                System.out.println("密码为：" + passWord);
                                System.out.println("破解成功，破解出来的密码为：" + breakPassWord);
                                l2.setText(breakPassWord);
                                break;
                            }
                        }
                    }
                });
                // 启动它
                t.start();
            }
        });
    }

    public static String generateCode() {

        Random r = new Random();

        StringBuilder verificationNum = new StringBuilder();
        // 生成四位
        for (int i = 1; i < 5; i++) {
            // 随机生成数字或者字母
            int randomCharNum = r.nextInt(2);
            if (randomCharNum == 0) {
                // 生成数字
                int num = r.nextInt(9);
                verificationNum.append(num);
            } else {
                // 生成字母
                // 随机生成大写或者小写
                int randomUpDown = r.nextInt(2);
                if (randomUpDown == 0) {
                    // 大写
                    char upNum = (char) (int) (Math.random() * (90 - 65 + 1) + 65);
                    verificationNum.append(upNum);
                } else {
                    // 小写
                    char downNum = (char) (int) (Math.random() * (122 - 97 + 1) + 97);
                    verificationNum.append(downNum);
                }
            }
        }
        return verificationNum + "";
    }
}
